package com.leonzx.base.controller.orders;

import com.leonzx.base.config.pager.Page;
import com.leonzx.base.config.pager.PageInfo;
import com.leonzx.base.controller.base.BaseController;
import com.leonzx.base.entity.system.Orders;
import com.leonzx.base.service.system.OrdersService;
import com.leonzx.base.service.system.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @Author:张勇
 * @Description:
 * @Date: 2018-06-11 9:27
 */
@RestController
@RequestMapping("/ajax/orders")
public class AjaxOrdersController extends BaseController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private UserService userService;

    /**
     * 委托單列表
     *
     * @param ordersRole
     * @param page
     * @return
     */
    @RequestMapping("/list")
    public Map<String, Object> list(Orders ordersRole, @PageInfo(pageSize = Page.PAGE_SIZE, orderBy = "create_time") Page<Orders> page) {
        Map<String, Object> result = getMap();
        try {
            List<Orders> baseBjbjjjes = ordersService.getAllBaseOrders(ordersRole);
            page.setResult(baseBjbjjjes);
            SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
            //循环list，处理数据
            for (Orders b : baseBjbjjjes) {
                String createTimeStr = "";
                if (b.getCreateTime() != null) {
                    createTimeStr = sdf.format(b.getCreateTime());
                }
                String createUserName = userService.getUserNameById(b.getCreateUserId());
                b.setCreateTimeStr(createTimeStr);
                b.setCreateUserName(createUserName);
            }
            result.put("count", page.getTotalCount());
            result.put("data", baseBjbjjjes);
            result.put("code", "0");
            result.put("msg", "加载完成");
        } catch (Exception e) {
            logger.error("获取委托单数据失败", e);
            addFailMessage(result, "获取委托单数据失败");
        }
        return result;
    }
    @RequestMapping("/saveGroupMessage")
    public Map<String, Object> saveGroupMessage(String number, String content ) {
        Map<String, Object> result = getMap();
        try {
            ordersService.saveGroupMessage(number ,content, currentUser().getId());
            addSuccessMessage(result, "保存群组成功");
        } catch (Exception e) {
            logger.error("保存群组失败 "+number+"", e);
            addFailMessage(result, "保存群组失败");
        }
        return result;
    }

}
